package cn.wolfcode.wolf2w.controller;


import cn.wolfcode.wolf2w.annotation.RequireLogin;
import cn.wolfcode.wolf2w.annotation.UserParam;
import cn.wolfcode.wolf2w.domain.Destination;
import cn.wolfcode.wolf2w.domain.Travel;
import cn.wolfcode.wolf2w.domain.TravelComment;
import cn.wolfcode.wolf2w.domain.UserInfo;
import cn.wolfcode.wolf2w.query.TravelQuery;
import cn.wolfcode.wolf2w.redis.IUserInfoRedisService;
import cn.wolfcode.wolf2w.service.IDestinationService;
import cn.wolfcode.wolf2w.service.ITravelCommentService;
import cn.wolfcode.wolf2w.service.ITravelService;
import cn.wolfcode.wolf2w.service.impl.TravelCommentServiceImpl;
import cn.wolfcode.wolf2w.util.JsonResult;
import cn.wolfcode.wolf2w.util.UMEditorUploader;
import com.alibaba.fastjson.JSON;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;

@RestController
@RequestMapping("travels")
public class TravelController {

    @Autowired
    private ITravelService travelService;

    @Autowired
    private IDestinationService destinationService;

    @Autowired
    private IUserInfoRedisService userInfoRedisService;

    @Autowired
    private ITravelCommentService travelCommentService;

    @GetMapping("/query")
    public Object query(TravelQuery qo){

        Page<Travel> page = travelService.query(qo);
        return JsonResult.success(page);
    }

    @GetMapping("/detail")
    public Object detail(String id){

        Travel travel = travelService.detail(id);

        return JsonResult.success(travel);
    }

    //上传图片

    @RequestMapping("/contentImage")

    @ResponseBody

    public String uploadUEImage(MultipartFile upfile, HttpServletRequest request) throws Exception{

        UMEditorUploader up = new UMEditorUploader(request);

        String[] fileType = {".gif" , ".png" , ".jpg" , ".jpeg" , ".bmp"};

        up.setAllowFiles(fileType);

        up.setMaxSize(10000); //单位KB

        up.upload(upfile);

        String callback = request.getParameter("callback");

        String result = "{\"name\":\""+ up.getFileName() +"\", \"originalName\": \""+ up.getOriginalName() +"\", \"size\": "+ up.getSize()

                +", \"state\": \""+ up.getState() +"\", \"type\": \""+ up.getType() +"\", \"url\": \""+ up.getUrl() +"\"}";

        result = result.replaceAll( "\\\\", "\\\\" );

        if(callback == null ){

            return result ;

        }else{

            return "<script>"+ callback +"(" + result + ")</script>";

        }

    }

    @RequireLogin
    @GetMapping("/input")
    public Object input(String id){

        HashMap<Object, Object> map = new HashMap<>();

        List<Destination> list = destinationService.list();

        if (id != null) {
            Travel travel = travelService.get(id);
            map.put("tv", travel);
        }

        map.put("dests", list);

        return JsonResult.success(map);
    }

    @PostMapping("/saveOrUpdate")
    public Object saveOrUpdate(Travel travel, HttpServletRequest request){

        String token = request.getHeader("token");

        UserInfo user = userInfoRedisService.getUserInfoByToken(token);

        travel.setUserId(user.getId());

        travelService.saveOrUpdate(travel);

        return JsonResult.success(travel.getId());
    }



    //测试参数解析器
    @GetMapping("/info")
    public Object info(@UserParam  UserInfo userInfo){

        if (userInfo == null){

            return JsonResult.success(new UserInfo());
        }

        return JsonResult.success(userInfo);
    }

    @GetMapping("/update")
    public void update(UserInfo userInfo){

        System.out.println(userInfo);
    }

    @PostMapping("/commentAdd")
    public Object commentAdd(TravelComment travelComment, @UserParam UserInfo userInfo){

        BeanUtils.copyProperties(userInfo, travelComment);

        travelComment.setUserId(userInfo.getId());

        travelCommentService.save(travelComment);

        return JsonResult.success();
    }

    @GetMapping("/comments")
    public Object comments(String travelId){

        List<TravelComment> comments = travelCommentService.comments(travelId);
        return JsonResult.success(comments);
    }
}
